package com.yaoayao.mall.all.controller;

import com.yaoayao.mall.activity.client.ActivityFeignClient;
import com.yaoayao.mall.common.result.Result;
import com.yaoayao.mall.model.activity.SeckillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;
    //秒杀频道 seckill.gmall.com
    @GetMapping("/index")
    public String secKillIndex(Model model){

        List<SeckillGoods> seckillGoodsList = activityFeignClient.findAll();
        model.addAttribute("list",seckillGoodsList);
        return "seckill/index";
    }

    //秒杀详情页面
    @GetMapping("/seckill/{skuId}.html")
    public String secKillDetail(@PathVariable Long skuId, Model model){
        SeckillGoods seckillGoods = activityFeignClient.findBySkuId(skuId);
        model.addAttribute("item",seckillGoods);
        return "seckill/item";
    }

    //开始秒杀
    @GetMapping("/seckill/queue.html")
    public String queue(Long skuId,String skuIdStr,Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);
        return "seckill/queue";
    }

    /**
     * 确认订单
     * @param model
     * @return
     */
    @GetMapping("seckill/trade.html")
    public String trade(Model model) {
        Result<Map<String, Object>> result = activityFeignClient.trade();
        if (result.isOk()) {
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        } else {
            model.addAttribute("message", result.getMessage());

            return "seckill/fail";
        }
    }
}
